home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / freeWAIS-sf-1.1 / ir / irsparse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-19  |  1.4 KB  |  81 lines

  1.  
  2. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  3.  
  4. #define s_malloc malloc
  5. #include <stdio.h>
  6. #include "irsparse.h"
  7. /*
  8. $Log: irsparse.c,v $
  9.  * Revision 1.2  1994/03/08  20:44:21  pfeifer
  10.  * Patchlevel 04
  11.  *
  12.  * Revision 1.2  1993/06/04  10:25:57  pfeifer
  13.  * Pachtlevel BIBDB
  14.  *
  15.  * Revision 1.1  1993/02/16  20:40:24  freewais
  16.  * Initial revision
  17.  *
  18. */
  19.  
  20. #define NEW(type) (type*)(s_malloc(sizeof(type)))
  21.  
  22.  
  23. sparse_array* make_sparse_array(size)
  24.      int size;
  25. {
  26.   sparse_array* tmp;
  27.   tmp = (sparse_array* )s_malloc(sizeof(sparse_array));
  28.   tmp->table = (doclist**)s_malloc(sizeof(doclist*)*size);
  29.   tmp->buckets = size;
  30.   return tmp;
  31. }
  32.  
  33. double* sparse_aref(array,index)
  34.      sparse_array* array;
  35.      long index;
  36. {
  37.   int hash;
  38.   doclist** ptr;
  39.   hash = index % array->buckets;
  40.   ptr = &array->table[hash];
  41.   while(*ptr != NULL) {
  42.  
  43.     if((*ptr)->item ==index) {
  44.       return &(*ptr)->val;
  45.     }
  46.  
  47.     if((*ptr)->item > index) {
  48.       doclist* tmp;
  49.       tmp = NEW(doclist);
  50.       tmp->val = 0.0;
  51.       tmp->item = index;
  52.       tmp->next = *ptr;
  53.       *ptr = tmp;
  54.       return &tmp->item;
  55.     }
  56.     ptr = &(*ptr)->next;
  57.   }
  58.   *ptr = NEW(doclist);
  59.   (*ptr)->item =index;
  60.   (*ptr)->val = 0.0;
  61.   (*ptr)->next = 0;
  62.   return &(*ptr)->item;
  63. }
  64.  
  65.  
  66. main() {
  67.   
  68.   sparse_array *foo;
  69.   int t;
  70.   foo = make_sparse_array(1024);
  71.   
  72.   for(t=0;t<10000;t++) {
  73.     long *x;
  74.     
  75.     x = sparse_aref(foo,t);
  76.     *x = t;
  77.   }
  78.  
  79.   printf("%ld is aref(2000)\n",*(sparse_aref(foo,2000)));
  80. }
  81.